<template>
  <div>
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin: 0 0 20px 0"
    >
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>留言反馈管理</el-breadcrumb-item>
      <el-breadcrumb-item>留言反馈列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="card_box">
      <div class="header_box">
        <el-input
          style="width: 168px; margin-right: 24px"
          v-model="query.key.createdBy"
          placeholder="请输入留言者名字"
          clearable
        ></el-input>

        <el-button type="primary" @click="getMessageList"> 确认搜索 </el-button>
      </div>

      <el-table :data="initData" style="width: 100%" border>
        <el-table-column label="创建日期" width="180">
          <template slot-scope="scope">
            {{
              $moment(parseInt(scope.row.createdAt)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </template>
        </el-table-column>
        <el-table-column prop="createdBy" label="留言人" width="120">
        </el-table-column>
        <el-table-column prop="message" label="内容"> </el-table-column>

        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button @click="clickView(scope.row)" type="text" size="small">
              查看
            </el-button>

            <el-button
              @click="clickReply(scope.row)"
              type="text"
              size="small"
              v-show="!scope.row.updatedAt"
            >
              回复
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="text-align: center; margin-top: 24px"
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleSizeChange"
        :page-size="query.pageSize"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="提示" :visible.sync="isView" width="400px">
      <div class="dia_box">
        <div class="created_time">
          <span>留言人： {{ infoData.createdBy }}</span>
          <span
            >时间：{{
              $moment(parseInt(infoData.createdAt)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </span>
        </div>
        <div class="msg_content">留言内容：{{ infoData.message }}</div>
      </div>

      <div class="dia_box bor_b" v-if="infoData.updatedAt">
        <div class="created_time">
          <span>回复人： {{ infoData.adminer }}</span>
          <span
            >时间：{{
              $moment(parseInt(infoData.updatedAt)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </span>
        </div>
        <div class="msg_content">回复内容：{{ infoData.adminMessage }}</div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isView = false"> 确 定 </el-button>
      </span>
    </el-dialog>

    <el-dialog title="提示" :visible.sync="isReply" width="400px">
      <div class="dia_box">
        <div class="created_time">
          <span>留言人： {{ infoData.createdBy }}</span>
          <span
            >时间：{{
              $moment(parseInt(infoData.createdAt)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </span>
        </div>
        <div class="msg_content">留言内容：{{ infoData.message }}</div>
        <span class="admin_title"> 回复内容： </span>
        <el-input
          type="textarea"
          :rows="4"
          placeholder="请输入内容"
          v-model="infoData.adminMessage"
        >
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="clickReplyVerify"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDeleteErrand,
  getErrandEdit,
  getMessageList,
  getMessageEdit,
} from "@/util/message.js";

export default {
  data() {
    return {
      query: {
        key: {
          createdBy: "",
        },
        pageSize: 10,
        pageNum: 1,
      },
      initData: [],
      infoData: {},
      total: 10,
      isView: false,
      isReply: false,
    };
  },
  created() {
    this.getMessageList();
  },
  mounted() {},
  methods: {
    // 查看
    clickView(e) {
      this.isView = true;
      this.infoData = e;
    },
    // 回复
    clickReply(e) {
      this.isReply = true;
      this.infoData = e;
    },
    // 关闭留言对话框
    close() {
      this.isReply = false;
      this.infoData.adminer = "";
      this.infoData.updatedAt = "";
      this.infoData.adminMessage = "";
    },
    // 回复 留言
    async clickReplyVerify() {
      this.infoData.adminer = window.localStorage.getItem("userName");
      this.infoData.updatedAt = new Date().getTime();
      let { data: res } = await getMessageEdit(this.infoData);
      if (res.status != 2000) {
        return this.$message.error(res.meg);
      }
      this.$message.success("回复成功");
      this.close();
    },
    // 获取数据
    async getMessageList() {
      let { data: res } = await getMessageList(this.query);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data.list;
      this.total = res.data.total;
    },

    // 分页
    handleSizeChange(e) {
      this.query.pageNum = e;
      this.getMessageList();
    },
  },
};
</script>
<style lang="less" scoped>
.card_box {
  min-height: calc(100vh - 200px);
  .header_box {
    display: flex;
    margin-bottom: 24px;
  }
  /deep/.cell {
    max-height: 50px;
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    img {
      height: 50px;
    }
  }
}

.dia_box {
  width: 350px;
  margin: 0 auto;
  .created_time {
    margin-bottom: 12px;
    span {
      margin-right: 12px;
    }
  }
  .msg_content {
    margin-bottom: 12px;
    line-height: 1.5;
  }
  .admin_title {
    display: inline-block;
    margin-bottom: 12px;
  }
}
.bor_b {
  padding-top: 12px;
  border-top: 1px solid #ccc;
}
</style>
